package com.atguigu.ucenter.service.impl;

import com.atguigu.commonutils.result.JWTUtils;
import com.atguigu.servicebase.exception.MyException;
import com.atguigu.ucenter.vo.LoginVo;
import com.atguigu.ucenter.vo.RegisterVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.ucenter.entity.UcenterMember;
import com.atguigu.ucenter.service.UcenterMemberService;
import com.atguigu.ucenter.mapper.UcenterMemberMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sun.security.provider.MD5;

import java.beans.Encoder;
import java.util.Map;

/**
 * @author kabuda
 * @description 针对表【ucenter_member(会员表)】的数据库操作Service实现
 * @createDate 2023-05-12 23:06:30
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember>
        implements UcenterMemberService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        //校验参数
        if (StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(mobile)) {
            throw new MyException(20001, "参数传递错误");
        }

        //判断电话是否存在数据库，如果不存再抛出异常
        LambdaQueryWrapper<UcenterMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UcenterMember::getMobile, mobile);
        UcenterMember member = this.getOne(wrapper);
        if (member == null) {
            throw new MyException(20001, "该用户未注册");
        }
        //判断密码是否正确
        if (!DigestUtils.md5Hex(password).equals(member.getPassword())) {
            throw new MyException(20001, "密码错误");
        }

        //校验是否被禁用
        if (member.getIsDisabled() == 1) {
            throw new MyException(20001, "该用户禁用");
        }
        //生成token返回前端
        String token = JWTUtils.createToken(String.valueOf(member.getId()), member.getNickname());
        return token;
    }

    @Override
    public void registerVo(RegisterVo registerVo) {
        //获取注册信息，进行校验
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        //校验参数
        if (StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code)) {
            throw new MyException(20001, "参数传递错误");
        }

        //比对验证码
        String s = redisTemplate.opsForValue().get(mobile);
        if (!s.equals(code)) {
            throw new MyException(20001, "验证码错误，请重新获取");
        }

        // 校验手机号
        //判断电话是否存在数据库，如果不存再抛出异常
        LambdaQueryWrapper<UcenterMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UcenterMember::getMobile, mobile);
        Integer member = this.count(wrapper);
        if (member > 0) {
            throw new MyException(20001, "该用户已经注册");
        }

        //添加到数据库
        //添加注册信息到数据库
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setNickname(nickname);
        ucenterMember.setMobile(registerVo.getMobile());
        ucenterMember.setPassword(DigestUtils.md5Hex(password));
        ucenterMember.setIsDisabled(0);
        ucenterMember.setAvatar("https://guliedu-kabuda.oss-cn-beijing.aliyuncs.com/2023/05/09/002453-1665764693c78a.jpg");
        this.save(ucenterMember);
    }

    @Override
    public UcenterMember getMemberByToken(String token) {
        Map<String, Object> map = JWTUtils.checkToken(token);
        String id = (String) map.get("id");
        UcenterMember member = this.getById(id);
        return member;
    }

    //根据openid判断
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer registerCount(String date) {
       Integer count= baseMapper.registerCount(date);
        return count;
    }

    public static void main(String[] args) {
        String kabuda = DigestUtils.md5Hex("kabuda");
        System.out.println(
                kabuda
        );
    }
}




